package com.baizhi.service.impl;

import com.baizhi.entity.Product;
import com.baizhi.mapper.ProductMapper;
import com.baizhi.service.ProductService;
import com.baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    @Override
    public List<Product> selectProducts(Integer pageNum, Integer pageSize) {
        List<Product> list = new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            list = mapper.selectProducts(pageNum, pageSize);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return list;
    }

    @Override
    public Product selectProductById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        Product p = null;
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            p = mapper.selectProductById(id);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return p;
    }

    @Override
    public void insertProduct(Product p) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            mapper.insertProduct(p);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void deleteProductById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            mapper.deleteProductById(id);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void updateProduct(Product p) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            mapper.updateProduct(p);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }
}
